/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.koron.risk.bean.DeptSafetyStaff;
import com.koron.risk.bean.DeptSafetyStaffBean;
import com.koron.risk.domain.GridNode;

public interface GridNodeMapper {

    @Insert("insert into grid_node(id,add_user,add_user_name,add_time,name,code,serial,subnumber,structure_code,corp_code,pid,qrcode_url,safety_staff_code,business_staff_code," +
            "safety_staff_name,business_staff_name, safety_leader,safety_leader_name,safety_officer,safety_officer_name) "
            + "values (#{id},#{addUser},#{addUserName},#{addTime},#{name},#{code},#{serial},#{subnumber},#{structureCode},#{corpCode},#{pid},#{qrcodeUrl},#{safetyStaffCode},#{businessStaffCode}," +
            "#{safetyStaffName},#{businessStaffName},#{safetyLeader},#{safetyLeaderName},#{safetyOfficer},#{safetyOfficerName})")
    Integer insert(GridNode gridNode);

    @Insert("<scrpit>"
            + "insert into grid_node(id,add_user,add_user_name,add_time,name,code,serial,subnumber,structure_code,corp_code,pid,qrcode_url,safety_staff_code,business_staff_code,safety_staff_name,business_staff_name) values "
            + "<foreach collection ='beanList' item='item' index='index' separator =','>"
            + "(#{item.id},#{item.addUser},#{item.addUserName},#{item.addTime},#{item.name},#{item.code},#{item.serial},#{item.subnumber},#{item.structureCode},#{item.corpCode},#{item.pid},#{item.qrcodeUrl},#{item.safetyStaffCode},#{item.businessStaffCode},#{item.safetyStaffName},#{item.businessStaffName})"
            + "</foreach>"
            + "</script>")
    Integer batchInsert(@Param("beanList") List<GridNode> beanList);

    @Update("update grid_node set update_time=#{updateTime},update_user=#{updateUser},update_user_name=#{updateUserName},name=#{name},serial=#{serial},subnumber=#{subnumber},qrcode_url=#{qrcodeUrl}," +
            "safety_staff_code=#{safetyStaffCode},business_staff_code=#{businessStaffCode},safety_staff_name=#{safetyStaffName},business_staff_name=#{businessStaffName}," +
            "safety_leader=#{safetyLeader},safety_leader_name=#{safetyLeaderName},safety_officer=#{safetyOfficer},safety_officer_name=#{safetyOfficerName} where id=#{id}")
    Integer update(GridNode gridNode);

    @Delete("delete from grid_node where id =#{id}")
    Integer delete(@Param("id") String id);

    @Select("select count(*) from grid_node where pid=#{id}")
    Integer subsetCount(@Param("id") String id);

    @Select("select a.*,b.`name` pnmae from grid_node a left join grid_node b on a.pid=b.id where a.id=#{id}")
    GridNode queryById(@Param("id") String id);

    @Select("select * from grid_node where pid=#{id} order by serial")
    List<GridNode> queryByPid(@Param("id") String id);

    @Select("select count(*) from grid_node where pid=#{id}")
    Integer queryCountByPid(@Param("id") String id);

    @Select("select * from grid_node where corp_code=#{corpCode} order by structure_code")
    List<GridNode> queryByCorpCode(@Param("corpCode") String corpCode);

    @Select("select * from grid_node where code=#{code}")
    GridNode queryByCode(@Param("code") String code);

    @Insert("insert into dept_safety_staff(dept_code,safety_staff_code,business_staff_code,safety_staff_name,business_staff_name,safety_leader,safety_leader_name,safety_officer,safety_officer_name)"
            + " values(#{deptCode},#{safetyStaffCode},#{businessStaffCode},#{safetyStaffName},#{businessStaffName},#{safetyLeader},#{safetyLeaderName},#{safetyOfficer},#{safetyOfficerName})")
    Integer setStaff(@Param("deptCode") String deptCode, @Param("safetyStaffCode") String safetyStaffCode, @Param("safetyStaffName") String safetyStaffName,
                     @Param("businessStaffCode") String businessStaffCode, @Param("businessStaffName") String businessStaffName, @Param("safetyLeader") String safetyLeader, @Param("safetyLeaderName") String safetyLeaderName, @Param("safetyOfficer") String safetyOfficer, @Param("safetyOfficerName") String safetyOfficerName);

    @Update("update dept_safety_staff set safety_staff_code=#{safetyStaffCode},business_staff_code=#{businessStaffCode},safety_staff_name=#{safetyStaffName},business_staff_name=#{businessStaffName}," +
            "safety_leader=#{safetyLeader},safety_leader_name=#{safetyLeaderName},safety_officer=#{safetyOfficer},safety_officer_name=#{safetyOfficerName}"
            + " where dept_code=#{deptCode}")
    Integer updateStaff(@Param("deptCode") String deptCode, @Param("safetyStaffCode") String safetyStaffCode, @Param("safetyStaffName") String safetyStaffName,
                        @Param("businessStaffCode") String businessStaffCode, @Param("businessStaffName") String businessStaffName, @Param("safetyLeader") String safetyLeader, @Param("safetyLeaderName") String safetyLeaderName, @Param("safetyOfficer") String safetyOfficer, @Param("safetyOfficerName") String safetyOfficerName);

    @Select("select count(*) from dept_safety_staff where dept_code=#{deptCode}")
    Integer countDept(@Param("deptCode") String deptCode);

    @Select("select * from dept_safety_staff where dept_code=#{deptCode}")
    DeptSafetyStaffBean deptStaff(@Param("deptCode") String deptCode);

    @Select("select * from dept_safety_staff where dept_code=#{deptCode}")
    DeptSafetyStaff queryDeptSafetyStaff(@Param("deptCode") String deptCode);

    @Select("<script>" +
            "select * from grid_node" +
            "<where>" +
            "<if test='name !=null and !name.isEmpty()'>" +
            " and name like concat('%',#{name},'%')" +
            "</if>" +
            "<if test='idList!=null'>" +
            " and id in" +
            "<foreach collection='idList' item='id' open='(' close=')' separator=','>" +
            "#{id}" +
            "</foreach>" +
            "</if>" +
            "</where>" +
            "</script>")
    List<GridNode> nodeListByIdListAndName(@Param("idList") List<String> idList, @Param("name") String name);

}
